<?php
session_start();

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . 'application/vendor/');

require_once "gapi/src/apiClient.php";
require_once "gapi/src/contrib/apiLatitudeService.php";

$username="root";
$password="";
$database="bustrack";


$apiClient = new apiClient();
$latitude = new apiLatitudeService($apiClient);

// If a oauth token was stored in the session, use that- and otherwise go through the oauth dance
/*if (isset($_SESSION['auth_token'])) {
        $apiClient->setAccessToken($_SESSION['auth_token']);
        $token = $_SESSION['auth_token'];
        $busId = $_SESSION['id'];
        $name = $_SESSION['name'];
        $query = "INSERT into access_tokens values($busId, '$name', '$token') on DUPLICATE key update token='$token'";
        
        //Update MySQL....
        if ($password != "")
          mysql_connect("localhost",$username,$password);
        else
          mysql_connect("localhost",$username);
          
        mysql_select_db($database) or die( "Unable to select database");
        mysql_query($query);
        mysql_close();
        session_destroy();
        echo "<br>Inserted the Token into the table ".$token;
} else {*/

        $busId = $_REQUEST['id'];
        $name = $_REQUEST['name'];
        if(!isset($busId) || !isset($name)) {
          echo "Please give bus ID and Name.."; 
          return;
        }
        $_SESSION['id'] = $busId;
        $_SESSION['name'] = $name;
        // In a real application this would be stored in a database, and not in the session!
        $token = $_SESSION['auth_token'] = $apiClient->authenticate();

        //Update MySQL....
        if ($password != "")
          mysql_connect("localhost",$username,$password);
        else
          mysql_connect("localhost",$username);
        $query = "INSERT into access_tokens values($busId, '$name', '$token') on DUPLICATE key update token='$token'";  
        mysql_select_db($database) or die( "Unable to select database");
        mysql_query($query);
        mysql_close();
        echo "<br>Inserted the Token into the table ".$token;
//}
$var = $latitude->getCurrentLocation();
echo "<br>User Current Location: ".print_r($var, TRUE);
?>
